//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#include <stdlib.h>
#include <memory>
#include <string>

#include "ShadowTask.h"

//==================================================================
using namespace PMath;

//==================================================================
ShadowTask::ShadowTask( RendDevice *RDevice, RendM5D::Scene *RScenep,
					    float NearZ, float FarZ ) :
	_RDevice(RDevice),
	_RScenep(RScenep),
	_OffscrTarget(0),
	_LightCamerap(0),
	_NearZ(NearZ),
	_FarZ(FarZ)
{
	// create the offscreen
	RendTarget::CreationParams	params;
	params.CREATE_COLOR_BUFFER = false;
	params.CREATE_DEPTH_BUFFER = true;

	_OffscrTarget = _RDevice->NewRendTarget( 1024, 1024, params );

	// create the camera for the scene
	_LightCamerap = _RScenep->NewCamera( _T("Camera") );
	_LightCamerap->SetViewport( 0, 0, 1024, 1024, 0, 1 );
	//_LightCamerap->SetPerspectiveProj( M_PI/3, 1.77f, _NearZ, _FarZ );
}

//==================================================================
void ShadowTask::RenderLightView( M5D::Light *lightp )
{
	static float hscale = 1000.0f / 2;

	lightp->UpdateGlobalTransform();
	_LightCamerap->SetOrthogonalProj( -hscale, hscale, -hscale, hscale, _NearZ, _FarZ );
	//_LightCamerap->SetPerspectiveProj( M_PI_2, 1, _NearZ, _FarZ );

	Vector4		global_dir = lightp->CalcGlobalPos();
	Matrix44	light_view_m;
	light_view_m = Matrix44::LookAt( (global_dir * hscale * 2.0f).GetVector3(),
										Vector3(0,0,0),
										Vector3(0,1,0) );

	_LightCamerap->SetMatrix( light_view_m );
	_LightCamerap->SetViewport( 0,0, _OffscrTarget->GetWidth(), _OffscrTarget->GetHeight() );

	_RScenep->SelectCamera( _LightCamerap );
	_RScenep->UpdatePose();


	//glBindTexture( GL_TEXTURE_2D, _OffscrTarget->GetDepthTexture()._tex_ogl_id );
	//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE );
	//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

	RendM5D::Params	rparams;
	rparams.use_lights = false;
	_RScenep->PreRender( rparams );
	_RDevice->RenderBegin( _OffscrTarget );
	_RScenep->Render( rparams );
	_RDevice->RenderEnd();

	//glBindTexture( GL_TEXTURE_2D, _OffscrTarget->GetDepthTexture()._tex_ogl_id );
	//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB );
}
